package com.lyc.service.impl;

import com.lyc.dao.BookShopDao;
import com.lyc.service.BookShopService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * @author ：Xiaochen
 * @date ：Created in 2022/11/15 16:05
 * @description：
 * @modified By：
 * @version:
 */
@Service
public class BookShopServiceImpl implements BookShopService {
    @Autowired
    private BookShopDao bookShopDao;
    public void purchase(String isbn, String username) {
        //根据图书编号查询图书对应的价格
        int price = bookShopDao.findBookPriceByIsbn(isbn);

        //判断库存是否够
        int stock = bookShopDao.findStockByIsbn(isbn);
        if (stock>0){
            //修改库存
            bookShopDao.updateBookStock(isbn);
        }else {
            throw new RuntimeException("图书编号为："+isbn+";库存不足");
        }
        //判断余额是否狗
        int balance = bookShopDao.findBalanceByUsername(username);
        if (balance>=price){
            //修改账户余额
            bookShopDao.updateAccount(username,price);
        }else {
            throw new RuntimeException("账户为："+username+";余额不足.");
        }

    }
}